using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using WebCrawlerTools.search;
using System.IO;

namespace WebCrawlerTools.data
{
    public class DDataInfo
    {

        private SortedList pageUrlList = new SortedList();
        public static void Store(DDataInfo datainfo, string path)
        {
            try
            {
                bool title = true;
                StreamWriter writer = new StreamWriter(path, true, Encoding.Default);
                foreach (DictionaryEntry de in datainfo.pageUrlList)
                {
                    SResult val = (SResult)de.Value;
                    StringBuilder sb = new StringBuilder();
                    if (title)
                    {
                        sb.AppendLine(val.Title.ToString());
                        title = false;
                    }
                    foreach (SLine l in val.Data)
                    {
                        writer.WriteLine(l.ToString());
                    }
                }
                writer.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }
        public void AddPageData(string url, SResult data)
        {
            if (!IsExist(url))
            {
                pageUrlList.Add(url, data);
            }
        }
        public bool IsExist(string url)
        {
            return pageUrlList.ContainsKey(url);
        }

        public void Clear()
        {
            pageUrlList.Clear();
        }

        public ArrayList GetSubPageUrlList()
        {
            ArrayList retVal = new ArrayList();
            foreach (SResult r in pageUrlList.GetValueList())
            {
                retVal.AddRange(r.GetValByKey("URL"));
            }
            return retVal;
        }
    }
}
